from pages.BasePage import BasePage


class IndexPage(BasePage):
    # 点击首页+按钮
    edit_btn=("xpath", '//*[@class="edit-btn"]')
    # 我的分类
    my_classify=("xpath", '//*[@class="my-classify-item"]')
    subclass_classify=("xpath", '//ul[@class="subclass-list"]')
    save_btn=("xpath", '//button[text()=" 完成 "]')
    recommend=("xpath", '//div[text()=" 推荐 "]')
    free_course_card=("xpath", '//div[@class="price-wrap"]/span[@class="free-price"]')
    course_detail=("xpath", '//div[@class="right"]/button')
    def get_my_classify(self):
        self.wait_find_element(*self.edit_btn).click()
        my_classify_items=self.wait_find_elements(*self.my_classify)
        my_classify_list=[]
        for i in my_classify_items:
            my_classify_list.append(i.text)
            print(i.text,end="\t")
        return my_classify_list
    def delete_my_classify(self,classify_name_list:list):
        for classify_name in classify_name_list:
            self.wait_find_element("xpath",f'//section[@class="el-drawer__body"]//div[text()=" {classify_name} "]/img').click()
            classify_name_list.remove(classify_name)
            break
    def get_subclass_classify(self):
        my_subclass_items=self.wait_find_elements(*self.subclass_classify)

        return my_subclass_items
    def add_my_classify(self,classify_subclass_items:list):
        for subclass_item in classify_subclass_items:
            print("语言学习分类",subclass_item)
            subclass_item.click()
        self.wait_find_element(*self.save_btn).click()

    def get_free_course_list(self):
        # 点击推荐tab
        self.wait_find_element(*self.recommend).click()
        free_course_eles=self.wait_find_elements(*self.free_course_card)
        print("免费课程列表", free_course_eles)
        if free_course_eles:
            for ele in free_course_eles:
                print(f"当前元素{ele}")
                # 点击其中的一门课程
                try:
                    ele.click()
                    # 进入课程详情页
                    # 获取所有窗口的句柄
                    all_windows = self.driver.window_handles
                    print("获取所有窗口的句柄", all_windows)
                    # 切换到当前最新打开的窗口

                    self.driver.switch_to.window(all_windows[-1])
                    print("当前窗口的句柄",self.driver.current_window_handle)

                    butt_ele = self.wait_find_element(*self.course_detail)
                    butt_ele_text = butt_ele.text
                    print(f"按钮的文本:{butt_ele_text}")
                    if butt_ele_text == "去学习":
                        print("该课程已购买")
                        self.driver.execute_script("window.close();")
                        self.driver.switch_to.window(all_windows[0])
                    else:
                        print("课程还未领取，可以进行点击领取")
                        butt_ele.click()
                        input()
                        msg = self.wait_find_element("xpath", '//div[@class="el-message-box__message"]/p').text
                        assert msg == "报名成功，即将跳转到学习页面~"
                        break
                except Exception as e:
                    self.driver.execute_script("arguments[0].scrollIntoView()", ele)
                    ele.click()




